
using UnityEngine;

public class PointToClick: MonoBehaviour
{
    [SerializeField] private float m_Duration = 1f; // 持续时间, 默认1s
    [SerializeField] private SpriteRenderer m_SpriteRenderer; // SpriteRenderer组件
    [SerializeField] private AnimationCurve m_ScaleCurve; // 缩放曲线

    private Vector3 m_InitialScale; // 初始大小
    private float m_Timer;
    private float m_FreqTimer; // 解决动画持续时间可能过长，但是动画曲线只有1s的情况

    void Start()
    {
        m_InitialScale = transform.localScale; // 记录初始大小
    }

    void Update()
    {
        m_Timer += Time.deltaTime;
        m_FreqTimer += Time.deltaTime;
        // m_FreqTimer %= 1f; // 相当于让当前曲线效果重复几次

        if (m_FreqTimer >= 1f)
        {
            m_FreqTimer = 0; // 这个应该mode运算性能更好
        }

        float scaleMultiplier = m_ScaleCurve.Evaluate(m_FreqTimer);
        transform.localScale = m_InitialScale * scaleMultiplier; // 根据曲线计算缩放比例并更新大小

        if (m_Timer >= m_Duration * 0.9f)
        {
            // 从0.9s到1s的过程中，逐渐消失
            float fadeProgress = (m_Timer - m_Duration * 0.9f) / (m_Duration * 0.1f);
            m_SpriteRenderer.color = new Color(1, 1, 1, 1-fadeProgress);
        }

        if (m_Timer >= m_Duration)
        {
            Destroy(gameObject);
        }
    }
}